package com.och.mrcp.core.rtp.session;

import lombok.Data;

import java.net.InetSocketAddress;

/**
 * RTP会话统计信息
 */
@Data
public class RtpSessionStats {
    
    // 会话基本信息
    private String sessionId;                   // 会话ID
    private InetSocketAddress localAddress;     // 本地地址
    private InetSocketAddress remoteAddress;    // 远程地址
    
    // RTP参数
    private int payloadType;                    // 负载类型
    private int sampleRate;                     // 采样率
    private int channels;                       // 声道数
    private int frameSize;                      // 帧大小
    private int ssrc;                           // 同步源标识符
    
    // 传输统计
    private long packetsSent;                   // 发送包数
    private long packetsReceived;               // 接收包数
    private long bytesSent;                     // 发送字节数
    private long bytesReceived;                 // 接收字节数
    
    // 时间信息
    private long lastActivityTime;              // 最后活动时间
    private long startTime;                     // 开始时间
    private long endTime;                       // 结束时间
    private long duration;                      // 持续时间
    
    // 会话状态
    private RtpSession.SessionState state;     // 会话状态
    
    /**
     * 计算发送速率 (包/秒)
     */
    public double getSendPacketRate() {
        if (duration == 0) {
            return 0.0;
        }
        return (double) packetsSent / (duration / 1000.0);
    }
    
    /**
     * 计算接收速率 (包/秒)
     */
    public double getReceivePacketRate() {
        if (duration == 0) {
            return 0.0;
        }
        return (double) packetsReceived / (duration / 1000.0);
    }
    
    /**
     * 计算发送带宽 (字节/秒)
     */
    public double getSendBandwidth() {
        if (duration == 0) {
            return 0.0;
        }
        return (double) bytesSent / (duration / 1000.0);
    }
    
    /**
     * 计算接收带宽 (字节/秒)
     */
    public double getReceiveBandwidth() {
        if (duration == 0) {
            return 0.0;
        }
        return (double) bytesReceived / (duration / 1000.0);
    }
    
    /**
     * 计算丢包率
     */
    public double getPacketLossRate() {
        long totalPackets = packetsSent + packetsReceived;
        if (totalPackets == 0) {
            return 0.0;
        }
        // 这里简化计算，实际应该基于序列号计算
        return 0.0;
    }
    
    /**
     * 计算平均包大小
     */
    public double getAveragePacketSize() {
        long totalPackets = packetsSent + packetsReceived;
        if (totalPackets == 0) {
            return 0.0;
        }
        return (double) (bytesSent + bytesReceived) / totalPackets;
    }
    
    /**
     * 检查会话是否活跃
     */
    public boolean isActive() {
        return state == RtpSession.SessionState.ACTIVE;
    }
    
    /**
     * 获取会话运行时间 (秒)
     */
    public double getRunningTimeSeconds() {
        return duration / 1000.0;
    }
    
    @Override
    public String toString() {
        return String.format("RtpSessionStats{id=%s, state=%s, local=%s, remote=%s:%d, " +
                           "packets=%d/%d, bytes=%d/%d, duration=%.1fs, " +
                           "sendRate=%.1f pkt/s, recvRate=%.1f pkt/s}",
                sessionId, state, localAddress, 
                remoteAddress != null ? remoteAddress.getAddress().getHostAddress() : "null",
                remoteAddress != null ? remoteAddress.getPort() : 0,
                packetsSent, packetsReceived, bytesSent, bytesReceived,
                getRunningTimeSeconds(), getSendPacketRate(), getReceivePacketRate());
    }
}
